package com.ming.zhihuWebSpider.pipeline;

import com.ming.zhihuWebSpider.mapping.DouyuMapper;
import com.ming.zhihuWebSpider.model.DouyuLolInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;

@Component("DouyuPipeline")
public class DouyuPipeline implements Pipeline {

	@Autowired
	private DouyuMapper douyuMapper;
 
//	@Override
//	public void process(DouyuLolInfo t, Task task) {
//
//		System.out.println(t.getTitle());
//		//douyuMapper.insertSelective(t);
//	}

	@Override
	public void process(ResultItems resultItems, Task task) {
		String title = (String)resultItems.get("title");
		String name =  (String)resultItems.get("name");
		String url = (String)resultItems.get("url");
		String type = (String)resultItems.get("type");
		if(title != null && name != null && url != null && type != null){
			DouyuLolInfo douyuLolInfo = new DouyuLolInfo();
			douyuLolInfo.setTitle(title);
			douyuLolInfo.setName(name);
			douyuLolInfo.setUrl(url);
			douyuLolInfo.setTime(new Date());
			douyuLolInfo.setType(type);
			douyuMapper.insertSelective(douyuLolInfo);
		}
	}
}
